define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchantgoods/index' + location.search,
                    /*add_url: 'merchantgoods/add',
                    edit_url: 'merchantgoods/edit',
                    del_url: 'merchantgoods/del',*/
                    multi_url: 'merchantgoods/multi',
                    import_url: 'merchantgoods/import',
                    table: 'merchantgoods',
                }
            });

            var table = $("#table");
            table.on('load-success.bs.table', function (e, data) {
                $('label[for="merchant_id"]').parent().css('display','none');
                $('label[for="card_type_id"]').parent().css('display','none');
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                visible: false,
                showToggle: false,
                showColumns: false,
                search: false,
                showExport: false,
                escape: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'merchant_id', title: __('merchant_id'),visible: false},
                        {field: 'card_type_id', title: __('card_type_id'),visible: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'refer', title: __('Refer'), operate: false,
                            formatter:function (value,row,index) {
                                if(value!='' && value!=null){
                                    return Table.api.formatter.content.call(this, value, row, index);
                                }else{
                                    return "";
                                }
                            }
                        },
                        {
                            field: 'images',
                            title: __('Images'),
                            operate: false,
                            events: Table.api.events.images,
                            formatter: Table.api.formatter.images
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"0": __('Status 0'), "1": __('Status 1')},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'type_data',
                            title: __('Type_data'),
                            searchList: {"0": __('Type_data 0'), "1": __('Type_data 1')},
                            formatter: Table.api.formatter.normal, operate: false
                        },
                        {field: 'card_type.name',title: __('Card_type.name'), operate: "LIKE"},
                        {field: 'price', title: __('Price'), operate: false},
                        {field: 'cost_price',title: __('Cost_price'), operate: false,
                            formatter:function (row,index) {
                                if(index.card_type_id>0){
                                    return "";
                                }
                                return index.cost_price;
                            }
                        },
                        {field: 'number',title: __('Number'), operate: false,
                            formatter:function (row,index) {
                                if(index.card_type_id>0 || index.type_data==0){
                                    return "";
                                }
                                return index.number;
                            }
                        },
                        {field: 'lifespan',title: __('Lifespan'), operate: false,
                            formatter:function (row,index) {
                                if(index.start_time<=0) return "";
                                var str=index.start_time_text+"——"+index.end_time_text;
                                return str;
                            }
                        },
                        {field: 'start_time',title: __('Start_time'), operate: false,
                            formatter: Table.api.formatter.datetime,visible:false},
                        {field: 'end_time',title: __('End_time'), operate: false,
                            formatter: Table.api.formatter.datetime,visible:false},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            addclass: 'datetimerange',
                            autocomplete: false,
                            operate: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'see',
                                    title: __('查看'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    text: '查看',
                                    //icon: 'fa fa-folder-o',
                                    url: function (row, column) {
                                        var url = 'merchantgoods/see?id=' + row.id;
                                        return url;
                                    },
                                },

                            ], formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                goods: function (value, row, index) {
                    //这里手动构造URL
                    url = "merchantgoodsgoods?merchantgoods_id=" +  row.id;
                    this.url = url;
                    return Table.api.formatter.addtabs.call(this, value, row, index);
                },
            },
        }
    };
    return Controller;
});
$("[data-toggle='addresspicker']").data("callback", function(res){
    //其中res则是包含了address/lat/lng等信息的JSON对象
    $("#classify").val(res.address3)
});
